package com.dhh.sky.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.dhh.sky.R;
import com.dhh.sky.model.Operation;
import com.dhh.sky.util.Constant;
import com.dhh.sky.widget.OperationListAdapter;

public class OperationListActivity extends SherlockFragmentActivity{
	private OperationListAdapter adapter;
	private ListView listView;
	private ProgressReceiver pReceiver;
	private RefreshReceiver rReceiver;
	private IntentFilter pFilter;
	private IntentFilter rFilter;
	
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
        setContentView(R.layout.operation_list); 

        initView();
        
        pReceiver = new ProgressReceiver();
        rReceiver = new RefreshReceiver();
        pFilter = new IntentFilter(Constant.BOARDFAST_UPLOAD_PROGRESS);
        rFilter = new IntentFilter(Constant.BOARDFAST_UPLOAD_REFRESH);
	}
	
	@Override
	public void onResume(){
		super.onResume();
		registerReceiver(pReceiver, pFilter);
		registerReceiver(rReceiver, rFilter);		
	}
	
	@Override
	public void onPause(){
		super.onPause();
		unregisterReceiver(pReceiver);
		unregisterReceiver(rReceiver);
	}
	
	private void initView(){
		listView = (ListView) this.findViewById(R.id.operation_list);
		adapter = new OperationListAdapter(this);
		listView.setAdapter(adapter);
		
		initData();
	}
	
	private void initData(){
		adapter.loadOperation();
		adapter.notifyDataSetChanged();
	}
	
	protected void initActionBar(){
    	getSupportActionBar().setHomeButtonEnabled(true);     
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);   
	}
    
    private void refreshProgress(String path,int progress,boolean isDone){
    	try{
        	int position = -1;  
        	Operation operation =null;
        	for(int i=0; i<adapter.getCount(); i++){
        		Operation op = adapter.getItem(i);
        		if(op.getPath().equalsIgnoreCase(path)){
        			operation  = op;
        			position = i;
        			break;
        		}
        	}            	
        	
        	if(position>=0){
        		int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount();
        		int wantedChild = position - firstPosition;
        		if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
        			return;
        		}
        		
        		View itemView = listView.getChildAt(wantedChild);  
        		TextView status = (TextView) itemView.findViewById(R.id.oli_status);
        		if(Integer.valueOf(operation.getAction())==Operation.OP_UPLOAD){
    				status.setText("Uploading...");
				}else{
					status.setText("Downloading...");
				}
        		ProgressBar bar = (ProgressBar) itemView.findViewById(R.id.oli_progress);
        		bar.setVisibility(View.VISIBLE);
        		bar.setMax(100);
        		bar.setProgress(progress);
        		if(isDone){        			
        			if(progress>=100){
        				if(Integer.valueOf(operation.getAction())==Operation.OP_UPLOAD){
            				status.setText("Upload Done");
        				}else{
        					status.setText("Download Done");
        				}
        			}
        		}else{
        			if(Integer.valueOf(operation.getAction())==Operation.OP_UPLOAD){
        				status.setText("Failed to upload");
    				}else{
    					status.setText("Failed to download");
    				}
        		}       		        		
        	}
    	}catch(Exception e){
			Log.e(Constant.TAG, e.getMessage(),e);
    	}
    }
    
    protected class ProgressReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String path = intent.getExtras().getString(Constant.PARAM_PATH);
			int progress = intent.getExtras().getInt(Constant.PARAM_PROGRESS);
			boolean isDone = intent.getExtras().getBoolean(Constant.PARAM_DONE);
			if(path!=null){
				refreshProgress(path,progress,isDone);
			}
		}
	}
    
    protected class RefreshReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String path = intent.getExtras().getString(Constant.PARAM_PATH);
			Operation op = new Operation();
			op.setPath(path);
			adapter.removeItem(op);
		}
	}
}
